import { defineStore } from "pinia";

import {
    getDisinfectApi,
    exportDisinfectAPi
} from '@/Api/Disinfect'
import { downloadFileData } from "@/utils/downloadFile";
import { useCommon } from "./Common";
const CommonStore = useCommon()

export const useDisinfect = defineStore('disinfect', {
    state: (): DisinfectState => {
        return {
            loading: false,
            searchForm: {
                TimeArr: [],
                startData: '',
                endData: '',
                serial: null,
                department: '',
                illArea: '',
                departmentWard: [],

                current: 1,
                pageNo: 1,
                pageSize: 20,
                total: 0,
                showSizeChanger: true,
                pageSizeOptions: ['20', '30', '50', '100'],
                showTotal(total: number) { return `共 ${total} 条` },
                onChange(pageNo: number, pageSize: number) {
                    this.current = pageNo
                    this.pageNo = pageNo
                    this.pageSize = pageSize
                }
            },
            exportForm: {
                TimeArr: [],
                startData: '',
                endData: '',
                department: '',
                illArea: ''
            },
            tableData: []
        }
    },
    actions: {
        async getDisinfectData() {
            this.loading = true
            if (this.searchForm.TimeArr && this.searchForm.TimeArr.length) {
                this.searchForm.startData = this.searchForm.TimeArr[0]
                this.searchForm.endData = `${this.exportForm.TimeArr[1]} 23:59:59`
            } else {
                this.searchForm.startData = ''
                this.searchForm.endData = ''
            }

            if (this.searchForm.departmentWard != undefined && this.searchForm.departmentWard.length == 1) {
                this.searchForm.department = this.searchForm.departmentWard[0]
                this.searchForm.illArea = null
            } else if (this.searchForm.departmentWard != undefined && this.searchForm.departmentWard.length == 2) {
                this.searchForm.department = this.searchForm.departmentWard[0]
                this.searchForm.illArea = this.searchForm.departmentWard[1]
            } else {
                if (CommonStore.department != '检验科') {
                    this.searchForm.department = CommonStore.department
                    this.searchForm.illArea = CommonStore.illArea
                } else {
                    this.searchForm.department = ''
                    this.searchForm.illArea = ''
                }
            }

            try {
                const data = await getDisinfectApi(this.searchForm) as any
                if (data) {
                    this.tableData = data[0]
                    this.tableData.forEach((item, index) => {
                        item.index = (this.searchForm.pageNo! - 1) * this.searchForm.pageSize! + index + 1
                        if (item.illArea) {
                            item.department = `${item.department}-${item.illArea}`
                        }
                    })
                    this.searchForm.total = data[1][0].total
                }
            } finally {
                this.loading = false
            }
        },
        async exportData() {
            if (this.exportForm.TimeArr.length) {
                this.exportForm.startData = this.exportForm.TimeArr[0]
                this.exportForm.endData = `${this.exportForm.TimeArr[1]} 23:59:59`
            }

            this.exportForm.department = CommonStore.department == '检验科' ? '' : CommonStore.department
            this.exportForm.illArea = CommonStore.illArea


            const data = await exportDisinfectAPi(this.exportForm)

            if (data) {
                downloadFileData(data, '消毒记录.xlsx')
            }
        }
    }
})